package lzupdater.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

import lzupdater.com.CommunicationHub;
import lzupdater.com.HubComponent;
import lzupdater.gui.elements.LocalizedJButton;
import lzupdater.gui.elements.LocalizedJLabel;
import lzupdater.locale.Dictionary;
import lzupdater.locale.Locale;
import lzupdater.locale.LocaleListener;
import lzupdater.locale.LocalizedString;
import lzupdater.locale.Dictionary.LanguageItem;
import lzupdater.log.LoggerLevel;
import lzupdater.utils.LZUPreferences;
import lzupdater.utils.LZUPreferencesListener;
import lzupdater.utils.WoWPathGuesser;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Panel with the user preferences
 * 
 * @author Fraskor
 */
public class PreferencesPanel extends HubComponent implements
		LZUPreferencesListener, LocaleListener {

	private static final LocalizedString preferencesString = new LocalizedString("Preferences");
	
	private JFrame panel;
	private LZUPreferences preferences;

	private LocalizedJLabel languageLabel = new LocalizedJLabel("Language :");
	private LocalizedJLabel verboseLabel = new LocalizedJLabel("Verbosity :");
	private LocalizedJLabel pathToWoW = new LocalizedJLabel("Path to WoW :");

	private LocalizedJButton browseButton = new LocalizedJButton("Browse ...");
	
	private JTextField wowPathField = new JTextField();
	
	private JComboBox boxLanguage = new JComboBox();
	private JComboBox boxVerbose = new JComboBox();

	private ComboBoxModel langModel;
	private ComboBoxModel verboseModel;

	private MainGui mainGui;
	private boolean alreadyOpened = false;
	
	/**
	 * Constructor
	 * 
	 * @param hub
	 *            the hub to which this class must be registered
	 */
	public PreferencesPanel(CommunicationHub hub) {
		super(hub);
		panel = new JFrame() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 441818556252390465L;

			@Override
			public void setVisible(boolean b) {
				makeItVisibile(this);
				super.setVisible(b);
			}			
		};
		
		Dictionary.addLocaleListener(this);
		panel.setPreferredSize(new Dimension(400,150));
		panel.setMinimumSize(panel.getPreferredSize());
	}

	public String getKey() {
		return preferencesString.getKey();
	}

	public void localeChange(Locale locale) {
		this.panel.setTitle("LZRosterUpdater: " + preferencesString.toString());
	}

	private void makeItVisibile(JFrame frame) {
		if (!alreadyOpened) {
			int x = mainGui.getGui().getX();
			int y = mainGui.getGui().getY();
			
			frame.setLocation(x + 20, y + 20);
			alreadyOpened = true;
		}
	}



	/**
	 * {@inheritDoc}
	 */
	@Override
	public void initializeCommunication() {
		mainGui = hub.getComponent(MainGui.class);
		preferences = hub.getComponent(LZUPreferences.class);
		preferences.addPreferencesListener(this);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void initialize() {
		boxLanguage.setEditable(false);
		boxVerbose.setEditable(false);

		initialiseLanguageBox();
		initialiseVerboseBox();

		JPanel subPanel = new JPanel();

		subPanel.setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.insets = new Insets(2,2,2,2);
		c.anchor = GridBagConstraints.EAST;
		c.fill = GridBagConstraints.NONE;
		subPanel.add(languageLabel, c);

		c.gridx = 1;
		c.gridy = 0;
		c.anchor = GridBagConstraints.WEST;
		c.fill = GridBagConstraints.NONE;
		subPanel.add(boxLanguage, c);

		c.gridx = 0;
		c.gridy = 1;
		c.anchor = GridBagConstraints.EAST;
		c.fill = GridBagConstraints.NONE;
		subPanel.add(verboseLabel, c);

		c.gridx = 1;
		c.gridy = 1;
		c.anchor = GridBagConstraints.WEST;
		c.fill = GridBagConstraints.NONE;
		subPanel.add(boxVerbose, c);

		c.gridx = 0;
		c.gridy = 2;
		c.anchor = GridBagConstraints.EAST;
		c.fill = GridBagConstraints.NONE;
		subPanel.add(pathToWoW, c);
		
		c.gridx = 1;
		c.gridy = 2;
		c.weightx = 1;
		c.anchor = GridBagConstraints.EAST;
		c.fill = GridBagConstraints.HORIZONTAL;
		subPanel.add(wowPathField, c);
		wowPathField.setEditable(false);
		
		
		c.gridx = 2;
		c.gridy = 2;
		c.weightx = 0;
		c.anchor = GridBagConstraints.EAST;
		c.fill = GridBagConstraints.NONE;
		subPanel.add(browseButton, c);
		
		browseButton.setAction(new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = -1970742884501141150L;

			public void actionPerformed(ActionEvent arg0) {
				String path = WoWPathGuesser.getInstance().browseToRootPath();
				preferences.setWowRootPath(path);
			}
		});
		
		panel.getContentPane().add(subPanel);//.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel.pack();
		//		panel.add(subPanel);
	}

	/**
	 * @return the main component of the panel
	 */
	public JFrame getGui() {
		return panel;
	}

	/**
	 * {@inheritDoc}
	 */
	public void preferencesUpdated(LZUPreferences preferences) {
		setSelectedLanguage(preferences.getLang());
		setSelectedVerboseLevel(preferences.getLogLevel());
		wowPathField.setText(preferences.getWowRootPath());
	}

	private void initialiseLanguageBox() {
		LanguageItem[] langs = Dictionary.getAvailableLanguages();

		langModel = new DefaultComboBoxModel(langs);
		boxLanguage.setModel(langModel);

		boxLanguage.setAction(new AbstractAction() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -8741063158269778483L;

			public void actionPerformed(ActionEvent arg0) {
				Object item = boxLanguage.getSelectedItem();
				if (item instanceof LanguageItem) {
					preferences.setLang(((LanguageItem) item).getLocale()
							.toString());
				}
			}
		});

		setSelectedLanguage(preferences.getLang());
	}

	private void initialiseVerboseBox() {
		verboseModel = new DefaultComboBoxModel(LoggerLevel.getValuesForComboBox());
		boxVerbose.setModel(verboseModel);

		boxVerbose.setAction(new AbstractAction() {

			/**
			 * 
			 */
			private static final long serialVersionUID = -1970742884501141150L;

			public void actionPerformed(ActionEvent arg0) {
				Object item = boxVerbose.getSelectedItem();
				if (item instanceof LoggerLevel) {
					preferences.setLogLevel((LoggerLevel) item);
				}
			}

		});

		setSelectedVerboseLevel(preferences.getLogLevel());
	}

	private void setSelectedLanguage(String lang) {
		if (langModel == null)
			return;
		for (int i = 0; i < langModel.getSize(); i++) {
			Object item = langModel.getElementAt(i);
			if (item != null && item.equals(lang)) {
				langModel.setSelectedItem(item);
				break;
			}
		}
	}

	private void setSelectedVerboseLevel(LoggerLevel level) {
		if (verboseModel == null)
			return;
		for (int i = 0; i < verboseModel.getSize(); i++) {
			Object item = verboseModel.getElementAt(i);
			if (item != null && item.equals(level)) {
				verboseModel.setSelectedItem(item);
				break;
			}
		}
	}

}
